qx.Class.define("elpintao_comp.comp.clientes.compositeClientes",
{
	extend : qx.ui.container.Composite,
	construct : function (application)
	{
		this.base(arguments);
		
	this.setLayout(new qx.ui.layout.Canvas());
		
	this.addListenerOnce("appear", function(e){
		tbl.focus();
	});
	
	
	var rowDataCliente = null;
	
	
	var functionActualizar = function(id_cliente, texto) {
		var p = {texto: texto};
		var rpc = new qx.io.remote.Rpc("services/", "elpintao.Clientes");
		try {
			var resultado = rpc.callSync("leer_clientes", p);
		} catch (ex) {
			alert("Sync exception: " + ex);
		}
		
		tbl.setFocusedCell();
		tableModel.setDataAsMapArray(resultado, true);
		if (id_cliente!=null) {
			tbl.buscar("id_cliente", id_cliente);
		} else if (tableModel.getRowCount()>0){
			tbl.setFocusedCell(0, 0, true);
		}
	}
	
	
	this.add(new qx.ui.basic.Label("Filtrar:"), {left: 0, top: 3});
	var txtFiltrar = new qx.ui.form.TextField("");
	txtFiltrar.setLiveUpdate(true);
	txtFiltrar.addListener("changeValue", function(e){
		var texto = qx.lang.String.trim(txtFiltrar.getValue());
		if (texto=="") {
			functionActualizar();
		} else if (texto.length >= 3) {
			functionActualizar(null, texto);
		}
	});
	
	this.add(txtFiltrar, {left: 45, top: 0, right: "50%"});
	
	
	//Menu de contexto
	
	var commandAgregar = new qx.ui.core.Command("Insert");
	commandAgregar.addListener("execute", function(e){
		var win = new elpintao_comp.comp.clientes.windowCliente(application, null);
		win.addListener("aceptado", function(e){
			functionActualizar(e.getData());
			tbl.focus();
		});
		win.setModal(true);
		application.getRoot().add(win);
		win.center();
		win.open();
	});
	var commandModificar = new qx.ui.core.Command("Enter");
	commandModificar.setEnabled(false);
	commandModificar.addListener("execute", function(e){
		var win = new elpintao_comp.comp.clientes.windowCliente(application, rowDataCliente.id_cliente);
		win.addListener("aceptado", function(e){
			functionActualizar(e.getData());
			tbl.focus();
		});
		win.setModal(true);
		application.getRoot().add(win);
		win.center();
		win.open();
	});
	
	
	var menu = new controles.menu.Menu();
	var btnAgregar = new qx.ui.menu.Button("Nuevo...", null, commandAgregar);
	var btnModificar = new qx.ui.menu.Button("Modificar...", null, commandModificar);
	var btnCtaCte = new qx.ui.menu.Button("CtaCte...");
	btnCtaCte.setEnabled(false);
	btnCtaCte.addListener("execute", function(e){
		if (rowDataCliente.cta_cte==null) {
			var win = new elpintao_comp.comp.clientes.windowCtaCte(application, {id_cliente: rowDataCliente.id_cliente});
		} else {
			var win = new elpintao_comp.comp.clientes.windowCtaCte(application, rowDataCliente.cta_cte);
		}
		
		win.addListener("aceptado", function(e){
			functionActualizar(e.getData(), qx.lang.String.trim(txtFiltrar.getValue()));
			tbl.focus();
		});
		win.setModal(true);
		application.getRoot().add(win);
		win.center();
		win.open();
	});
	
	menu.add(btnAgregar);
	menu.add(btnModificar);
	menu.addSeparator();
	menu.add(btnCtaCte);
	menu.memorizar();
	
		
		
		//Tabla

		
		var tableModel = new qx.ui.table.model.Simple();
		tableModel.setColumns(["Razón social", "Tipo doc.", "Nro.doc", "Cond. IVA", "Domicilio", "Teléfono", "Cta.cte."], ["razon_social", "tipo_doc", "dni_cuit", "condicion_iva", "domicilio", "telefono", "cta_cte"]);
		//tableModel.setDataAsMapArray(resultado.contacto, true);

		var custom = {tableColumnModel : function(obj) {
			return new qx.ui.table.columnmodel.Resize(obj);
		}};
		
		var tbl = new controles.table.Table(tableModel, custom);
		//var tbl = this._tbl = new pediatras.controles.table.Table(tableModel, custom);
		tbl.getSelectionModel().setSelectionMode(qx.ui.table.selection.Model.SINGLE_SELECTION);
		tbl.toggleColumnVisibilityButtonVisible();
		tbl.toggleShowCellFocusIndicator();
		tbl.toggleStatusBarVisible();

		tbl.addListener("cellDblclick", function(e){
			commandModificar.execute();
		});
		
		
		var tableColumnModel = tbl.getTableColumnModel();
		//tableColumnModel.setColumnVisible(0, false);
		
		var cellrendererTipoDoc = new qx.ui.table.cellrenderer.Replace();
		cellrendererTipoDoc.setReplaceMap({
			"C": "CUIT",
			"L": "CUIL",
			"0": "Lib. enrolam.",
			"1": "Lib. cívica",
			"2": "D.N.I.",
			"3": "Pasaporte",
			"4": "Cédula ident."
		});
		tableColumnModel.setDataCellRenderer(1, cellrendererTipoDoc);
		
		var cellrendererRespIva = new qx.ui.table.cellrenderer.Replace();
		cellrendererRespIva.setReplaceMap({
			"I": "Resp. inscrip.",
			"E": "Exento",
			"A": "No responsable",
			"C": "Consumidor final",
			"M": "Monotributo"
		});
		tableColumnModel.setDataCellRenderer(3, cellrendererRespIva);
		
		var cellrendererCtaCte = new qx.ui.table.cellrenderer.Replace();
		cellrendererCtaCte.setReplaceFunction(function(value) {
			if (value == null) return "No"; else return "Si";
		});
		tableColumnModel.setDataCellRenderer(6, cellrendererCtaCte);
		
		
      // Obtain the behavior object to manipulate
		var resizeBehavior = tableColumnModel.getBehavior();
		//resizeBehavior.set(0, {width:"40%", minWidth:100});
		//resizeBehavior.set(1, {width:"20%", minWidth:100});
		//resizeBehavior.set(2, {width:"40%", minWidth:100});
		
		var selectionModel = tbl.getSelectionModel();
		selectionModel.addListener("changeSelection", function(e){
			var bool = !selectionModel.isSelectionEmpty()
			
			rowDataCliente = tableModel.getRowData(tbl.getFocusedRow());
			
			commandModificar.setEnabled(bool);
			btnCtaCte.setEnabled(bool);
			menu.memorizar([commandModificar, btnCtaCte]);
		});
		
		tbl.setContextMenu(menu);
		
		this.add(tbl, {left: 0, top: 25, right: 0, bottom: 0});
		
		
		
		
	functionActualizar();
		
		
	},
	members : 
	{
		
	}
});